package mailServlet;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.MailSendDAO;
import dto.MailSendDTO;
import dto.UserDTO;

@SuppressWarnings("serial")
public class MailReadServlet extends HttpServlet {

	public MailReadServlet() {
		super();
	}

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.execute(request, response);

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.execute(request, response);

	}
	
	public void execute(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
	    String basePath=request.getSession().getServletContext().getRealPath("\\")+"AllUsers";
	    UserDTO udto=(UserDTO) request.getSession().getAttribute("USER");
	    MailSendDAO mdao=new MailSendDAO();
	    MailSendDTO mdto=new MailSendDTO();
		StringBuffer Message=new StringBuffer();
	    String date=request.getParameter("date");
		String n=request.getParameter("n");
		String sql=null;
		if("1".equals(n)){
			Message=readMail(basePath+"/"+udto.getUserName()+"/receive/"+date+".txt");
			sql="update tb_send set receive_state='2' where send_toname='"+udto.getUserName()+"' and send_date='"+date+"'";
            if(mdao.ChangeState(sql)){
            	sql="select * from tb_send where send_toname='"+udto.getUserName()+"' and send_date='"+date+"'";
            	mdto=mdao.getDTO(sql);
            	request.setAttribute("mdto", mdto);
            	request.setAttribute("Message", Message);
            	request.setAttribute("n", n);
            	request.getSession().setAttribute("readOver", "MailReceiveServlet");
            	request.getRequestDispatcher("mailJsp/read.jsp").forward(request, response);
            }
			
		}else if("2".equals(n)){
			Message=readMail(basePath+"/"+udto.getUserName()+"/send/"+date+".txt");
			sql="select * from tb_send where send_fromname='"+udto.getUserName()+"' and send_date='"+date+"'";
        	mdto=mdao.getDTO(sql);
        	request.setAttribute("mdto", mdto);
        	request.setAttribute("Message", Message);
        	request.getSession().setAttribute("readOver", "MailHaveSendServlet");
        	request.getRequestDispatcher("mailJsp/read.jsp").forward(request, response);
		}else if("3".equals(n)){
			Message=readMail(basePath+"/"+udto.getUserName()+"/delete/"+date+".txt");
			sql="select * from tb_send where (send_fromname='"+udto.getUserName()+"' and send_state='3') or (send_toname='"+udto.getUserName()+"' and receive_state='3') "+"and send_date='"+date+"'";
        	mdto=mdao.getDTO(sql);
        	request.setAttribute("mdto", mdto);
        	request.setAttribute("Message", Message);
        	request.getSession().setAttribute("readOver", "MailHaveDeletedServlet");
        	request.getRequestDispatcher("mailJsp/read.jsp").forward(request, response);
		}else if("4".equals(n)){
			Message=readMail(basePath+"/"+udto.getUserName()+"/ready/"+date+".txt");
			sql="select * from tb_send where send_fromname='"+udto.getUserName()+"' and send_date='"+date+"'";
        	mdto=mdao.getDTO(sql);
        	request.setAttribute("mdto", mdto);
        	request.setAttribute("Message", Message);
        	request.setAttribute("n", n);
        	request.setAttribute("toName", mdto.getToName());
        	request.getRequestDispatcher("mailJsp/write.jsp").forward(request, response);
		}
	}

	private StringBuffer readMail(String fileName){
		StringBuffer Message=new StringBuffer();
		String str=null;
		BufferedReader br=null;
		try {
			br=new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
			while((str=br.readLine())!=null){
				Message.append(str);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return Message;
	}
	
	public void init() throws ServletException {
		// Put your code here
	}

}
